<?php

namespace App\Http\Controllers\Api;

use App\Services\GameChannelService;
use App\Http\Controllers\Controller;
use App\Services\WechatApi\WechatApiMessage;
use App\Services\WechatService;
use App\Services\WxMinigameLoginSessionService;
use Illuminate\Http\Request;

class WxMinigameController extends Controller
{
    // 检测文本内容是否合规
    public function msgCheck(Request $request)
    {
        $params = $request->input();
        $game_channel_id = $params['game_channel_id'];
        $game_channel = GameChannelService::getInstance()->get($game_channel_id);
        if (empty($game_channel)) {
            return yqok(['check_ok' => 0]);
        }

        $channel_openid = $params['channel_openid'];
        $login = WxMinigameLoginSessionService::getInstance()->getByUnionid($game_channel['appid'], $channel_openid);
        $scene = $params['scene'];
        $content = $params['content'];
        $access_token = WechatService::getInstance()->getAccessToken($game_channel['appid'], $game_channel['appsecret']);
        $res = WechatApiMessage::getInstance()->msgSecCheck($access_token, $login->openid, $scene, $content);
        if (!$res || $res['result']['label'] != 100) {
            return yqok(['check_ok' => 0]);
        }

        return yqok(['check_ok' => 1]);
    }
}
